﻿#include "FishingDialog.h"
#include "../../MirGraphics/MLibrary.h"
#include "../../MirSounds/SoundManager.h"
#include "../../../Shared/Enums.h"
#include "../../MirObjects/MapObject.h"
#include "../../../Shared/Data/ItemData.h"
#include "../GameScene.h"
#include "../../MirControls/MirMessageBox.h"
#include "../../../Shared/Language.h"
#include "../../Settings.h"
#include "../../MirControls/MirAnimatedButton.h"
#include "../../../Shared/ClientPackets.h"
#include "../../MirNetwork/Network.h"

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirNetwork;
using namespace Client::MirObjects;
using namespace Client::MirSounds;
namespace C = ClientPackets;
namespace S = ServerPackets;

namespace Client::MirScenes::Dialogs
{

    FishingDialog::FishingDialog()
    {
        setIndex(1340);
        setLibrary(Libraries::Prguse);
        setMovable(true);
        setSort(true);
        setLocation(getCenter());
        BeforeDraw += FishingDialog_BeforeDraw;

        TitleLabel = new MirLabel();
        Point tempVar(10, 4);
        TitleLabel->setLocation(&tempVar);
        TitleLabel->setDrawFormat(TextFormatFlags::VerticalCenter | TextFormatFlags::HorizontalCenter);
        TitleLabel->setParent(this);
        TitleLabel->setNotControl(true);
        Size tempVar2(180, 20);
        TitleLabel->setSize(&tempVar2);

        FishingRod = new MirControl();
        FishingRod->setParent(this);
        Point tempVar3(0, 30);
        FishingRod->setLocation(&tempVar3);
        FishingRod->setNotControl(true);
        FishingRod->BeforeDraw += FishingRod_BeforeDraw;

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(361);
        CloseButton->setIndex(360);
        Point tempVar4(175, 3);
        CloseButton->setLocation(&tempVar4);
        CloseButton->setLibrary(Libraries::Prguse2);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(362);
        CloseButton->setSound(SoundList::ButtonA);

        CloseButton->Click += [&] (o, e)
        {
            Hide();
        };

//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to the C# 'typeof' operator:
        Grid = std::vector<MirItemCell*>(Enum::GetNames(typeof(FishingSlot)).size());

        MirItemCell *tempVar5 = new MirItemCell();
        tempVar5->setItemSlot(static_cast<int>(FishingSlot::Hook));
        tempVar5->setGridType(MirGridType::Fishing);
        tempVar5->setParent(this);
        Size tempVar6(34, 30);
        tempVar5->setSize(&tempVar6);
        Point tempVar7(17, 203);
        tempVar5->setLocation(&tempVar7);
        Grid[static_cast<int>(FishingSlot::Hook)] = tempVar5;
        MirItemCell *tempVar8 = new MirItemCell();
        tempVar8->setItemSlot(static_cast<int>(FishingSlot::Float));
        tempVar8->setGridType(MirGridType::Fishing);
        tempVar8->setParent(this);
        Size tempVar9(34, 30);
        tempVar8->setSize(&tempVar9);
        Point tempVar10(17, 241);
        tempVar8->setLocation(&tempVar10);
        Grid[static_cast<int>(FishingSlot::Float)] = tempVar8;

        MirItemCell *tempVar11 = new MirItemCell();
        tempVar11->setItemSlot(static_cast<int>(FishingSlot::Bait));
        tempVar11->setGridType(MirGridType::Fishing);
        tempVar11->setParent(this);
        Size tempVar12(34, 30);
        tempVar11->setSize(&tempVar12);
        Point tempVar13(57, 241);
        tempVar11->setLocation(&tempVar13);
        Grid[static_cast<int>(FishingSlot::Bait)] = tempVar11;

        MirItemCell *tempVar14 = new MirItemCell();
        tempVar14->setItemSlot(static_cast<int>(FishingSlot::Finder));
        tempVar14->setGridType(MirGridType::Fishing);
        tempVar14->setParent(this);
        Size tempVar15(34, 30);
        tempVar14->setSize(&tempVar15);
        Point tempVar16(97, 241);
        tempVar14->setLocation(&tempVar16);
        Grid[static_cast<int>(FishingSlot::Finder)] = tempVar14;

        MirItemCell *tempVar17 = new MirItemCell();
        tempVar17->setItemSlot(static_cast<int>(FishingSlot::Reel));
        tempVar17->setGridType(MirGridType::Fishing);
        tempVar17->setParent(this);
        Size tempVar18(34, 30);
        tempVar17->setSize(&tempVar18);
        Point tempVar19(137, 241);
        tempVar17->setLocation(&tempVar19);
        Grid[static_cast<int>(FishingSlot::Reel)] = tempVar17;

        delete tempVar17;
        delete tempVar14;
        delete tempVar11;
        delete tempVar8;
        delete tempVar5;
    }

    void FishingDialog::FishingDialog_BeforeDraw(std::any sender, EventArgs *e)
    {
        UserItem *item = MapObject::User->Equipment[static_cast<int>(EquipmentSlot::Weapon)];

        if (MapObject::User->getHasFishingRod() && item != nullptr)
        {
            TitleLabel->setText(item->getFriendlyName());
        }
    }

    void FishingDialog::FishingRod_BeforeDraw(std::any sender, EventArgs *e)
    {
        int FishingImage = 0;
        if (MapObject::User->getHasFishingRod())
        {
            UserItem *rod = MapObject::User->Equipment[static_cast<int>(EquipmentSlot::Weapon)];

            if (GameScene::getUser()->Weapon == 49)
            {
                FishingImage = 1333;
            }
            else if (GameScene::getUser()->Weapon == 50)
            {
                FishingImage = 1335;
            }

            if (rod != nullptr && rod->Slots.size() >= 5 && rod->Slots[static_cast<int>(FishingSlot::Hook)] != nullptr)
            {
                FishingImage++;
            }
        }

        Point tempVar(getLocation()->X + 10, getLocation()->Y + 40);
        Libraries::StateItems->Draw(FishingImage, &tempVar, Color::White, false);
    }

    void FishingDialog::Show()
    {
        if (getVisible())
        {
            return;
        }

        if (!GameScene::getUser()->getHasFishingRod())
        {
            MirMessageBox *messageBox = new MirMessageBox(GameLanguage::NoFishingRod, MirMessageBoxButtons::OK);
            messageBox->Show();

            delete messageBox;
            return;
        }

        setVisible(true);
    }

    MirItemCell *FishingDialog::GetCell(unsigned long long id)
    {
        for (int i = 0; i < Grid.size(); i++)
        {
            if (Grid[i]->getItem() == nullptr || Grid[i]->getItem().UniqueID != id)
            {
                continue;
            }
            return Grid[i];
        }
        return nullptr;
    }

    FishingStatusDialog::FishingStatusDialog()
    {
        setIndex(1341);
        setLibrary(Libraries::Prguse);
        setMovable(true);
        setSort(true);
        Size tempVar(244, 128);
        setSize(&tempVar);
        Point tempVar2((Settings::ScreenWidth - getSize()->Width) / 2, 300);
        setLocation(&tempVar2);
        BeforeDraw += FishingStatusDialog_BeforeDraw;

        ChanceBar = new MirControl();
        ChanceBar->setParent(this);
        Point tempVar3(14, 64);
        ChanceBar->setLocation(&tempVar3);
        ChanceBar->setNotControl(true);
        ChanceBar->BeforeDraw += ChanceBar_BeforeDraw;

        ChanceLabel = new MirLabel();
        Point tempVar4(14, 62);
        ChanceLabel->setLocation(&tempVar4);
        Size tempVar5(216, 12);
        ChanceLabel->setSize(&tempVar5);
        ChanceLabel->setDrawFormat(TextFormatFlags::VerticalCenter | TextFormatFlags::HorizontalCenter);
        ChanceLabel->setParent(this);
        ChanceLabel->setNotControl(true);

        ProgressBar = new MirControl();
        ProgressBar->setParent(this);
        Point tempVar6(14, 79);
        ProgressBar->setLocation(&tempVar6);
        ProgressBar->setNotControl(true);
        ProgressBar->BeforeDraw += ProgressBar_BeforeDraw;

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(361);
        CloseButton->setIndex(360);
        Point tempVar7(216, 4);
        CloseButton->setLocation(&tempVar7);
        CloseButton->setLibrary(Libraries::Prguse2);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(362);
        CloseButton->setSound(SoundList::ButtonA);
        CloseButton->Click += [&] (o, e)
        {
            Cancel();
        };

        FishDisableButton = new MirImageControl();
        FishDisableButton->setIndex(149);
        Point tempVar8(47, 95);
        FishDisableButton->setLocation(&tempVar8);
        FishDisableButton->setLibrary(Libraries::Title);
        FishDisableButton->setParent(this);
        FishDisableButton->setNotControl(true);

        FishButton = new MirAnimatedButton();
        FishButton->Animated = true;
        FishButton->AnimationCount = 10;
        FishButton->Loop = true;
        FishButton->AnimationDelay = 130;
        FishButton->setIndex(170);
        FishButton->setPressedIndex(142);
        FishButton->setLibrary(Libraries::Title);
        FishButton->setParent(this);
        Point tempVar9(47, 95);
        FishButton->setLocation(&tempVar9);
        FishButton->setSound(SoundList::ButtonA);
        FishButton->setVisible(false);
        FishButton->Click += [&] (o, e)
        {
            C::FishingCast *tempVar10 = new C::FishingCast();
            tempVar10->CastOut = false;
            Network::Enqueue(tempVar10);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar10' statement was not added since tempVar10 was passed to a method or constructor. Handle memory management manually.
        };

        AutoCastButton = new MirButton();
        AutoCastButton->setIndex(180);
        AutoCastButton->setHoverIndex(181);
        AutoCastButton->setPressedIndex(182);
        Point tempVar11(110, 95);
        AutoCastButton->setLocation(&tempVar11);
        AutoCastButton->setLibrary(Libraries::Title);
        AutoCastButton->setParent(this);
        AutoCastButton->setSound(SoundList::ButtonA);
        AutoCastButton->Click += [&] (o, e)
        {
            if (_canAutoCast)
            {
                _autoCast = !_autoCast;
    
                //AutoCastTick.Visible = _autoCast;
                AutoCastBox->setIndex(_autoCast ? 1344 : 1343);
    
                C::FishingChangeAutocast *tempVar12 = new C::FishingChangeAutocast();
                tempVar12->AutoCast = _autoCast;
                Network::Enqueue(tempVar12);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar12' statement was not added since tempVar12 was passed to a method or constructor. Handle memory management manually.
            }
        };

        AutoCastBox = new MirImageControl();
        AutoCastBox->setIndex(1343);
        Point tempVar13(172, 95);
        AutoCastBox->setLocation(&tempVar13);
        AutoCastBox->setLibrary(Libraries::Prguse);
        AutoCastBox->setParent(this);

        ESCExitButton = new MirButton();
        ESCExitButton->setIndex(1346);
        ESCExitButton->setHoverIndex(1346);
        ESCExitButton->setPressedIndex(1346);
        Point tempVar14(135, 41);
        ESCExitButton->setLocation(&tempVar14);
        ESCExitButton->setLibrary(Libraries::Prguse);
        ESCExitButton->setParent(this);
        ESCExitButton->setSound(SoundList::ButtonA);
        ESCExitButton->Click += [&] (o, e)
        {
            bEscExit = !bEscExit;
            ESCTick->setVisible(bEscExit);
        };

        ESCTick = new MirImageControl();
        ESCTick->setIndex(1347);
        Point tempVar15(135, 41);
        ESCTick->setLocation(&tempVar15);
        ESCTick->setLibrary(Libraries::Prguse);
        ESCTick->setParent(this);
        ESCTick->setVisible(false);
        ESCTick->setNotControl(true);

        ESCExit = new MirImageControl();
        ESCExit->setIndex(45);
        Point tempVar16(150, 40);
        ESCExit->setLocation(&tempVar16);
        ESCExit->setLibrary(Libraries::Title);
        ESCExit->setParent(this);
        ESCExit->setNotControl(true);
    }

    void FishingStatusDialog::FishingStatusDialog_BeforeDraw(std::any sender, EventArgs *e)
    {
        bool oldCanAutoCast = _canAutoCast;

        if (MapObject::User->getHasFishingRod())
        {
            UserItem *rod = MapObject::User->Equipment[static_cast<int>(EquipmentSlot::Weapon)];

            if (rod == nullptr || rod->Slots.size() < 5 || rod->Slots[static_cast<int>(FishingSlot::Reel)] == nullptr)
            {
                _canAutoCast = false;
                AutoCastBox->setVisible(false);
                AutoCastButton->setVisible(false);
            }
            else
            {
                _canAutoCast = true;
                AutoCastBox->setVisible(true);
                AutoCastButton->setVisible(true);
            }
        }

        if (_autoCast && !_canAutoCast)
        {
            _autoCast = false;

            C::FishingChangeAutocast *tempVar = new C::FishingChangeAutocast();
            tempVar->AutoCast = _autoCast;
            Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void FishingStatusDialog::ChanceBar_BeforeDraw(std::any sender, EventArgs *e)
    {
        if (Libraries::Prguse == nullptr)
        {
            return;
        }

        int width;

        width = static_cast<int>(2.16 * ChancePercent);

        if (width < 0)
        {
            width = 0;
        }
        if (width > 216)
        {
            width = 216;
        }
        Rectangle *r = new Rectangle(0, 0, width, 12);
        Point tempVar(ChanceBar->getDisplayLocation()->X, ChanceBar->getDisplayLocation()->Y);
        Libraries::Prguse->Draw(1342, r, &tempVar, Color::White, false);

//C# TO C++ CONVERTER TODO TASK: A 'delete r' statement was not added since r was passed to a method or constructor. Handle memory management manually.
    }

    void FishingStatusDialog::ProgressBar_BeforeDraw(std::any sender, EventArgs *e)
    {
        if (Libraries::Prguse == nullptr)
        {
            return;
        }

        int width;

        width = static_cast<int>(2.16 * ProgressPercent);

        if (width < 0)
        {
            width = 0;
        }
        if (width > 216)
        {
            width = 216;
        }

        Rectangle *r = new Rectangle(0, 0, width, 8);
        Point tempVar(ProgressBar->getDisplayLocation()->X, ProgressBar->getDisplayLocation()->Y);
        Libraries::Prguse->Draw(1349, r, &tempVar, Color::White, false);

//C# TO C++ CONVERTER TODO TASK: A 'delete r' statement was not added since r was passed to a method or constructor. Handle memory management manually.
    }

    void FishingStatusDialog::Cancel()
    {
        if (getVisible())
        {
            C::FishingCast *tempVar = new C::FishingCast();
            tempVar->CastOut = false;
            Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        Hide();

    }
}
